<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

if ($user_id <= 0) {
    echo json_encode([
        'status' => 'error',
        'message' => '无效的用户ID'
    ]);
    exit;
}

try {
    $sql = "SELECT g.* FROM fitness_guides g 
            INNER JOIN favorites f ON g.id = f.guide_id 
            WHERE f.user_id = ? AND g.status = 1 
            ORDER BY f.created_at DESC";
            
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $favorites = [];
    while ($row = $result->fetch_assoc()) {
        // 格式化时间
        $row['create_time'] = date('Y-m-d H:i', strtotime($row['create_time']));
        $favorites[] = $row;
    }
    
    echo json_encode([
        'status' => 'success',
        'favorites' => $favorites
    ]);

} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage()
    ]);
} finally {
    if (isset($stmt)) $stmt->close();
    $conn->close();
}
?> 